package co.fitstart.weapp.api.web.controller.market;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.demo2do.core.entity.result.HttpResult;

import co.fitstart.entity.market.OfflineExperienceSignUp;
import co.fitstart.entity.market.OnlineExperienceSignUp;
import co.fitstart.entity.site.Site;
import co.fitstart.weapp.api.handler.SmsSender;
import co.fitstart.weapp.api.service.ExperienceSignUpService;
import co.fitstart.weapp.api.service.SiteService;

/**
 * 体验课报名
 * @author haiyan_xiao
 *
 */
@RestController
@RequestMapping("/experience-sign-up")
public class ExperienceSignUpController {

    private static final Logger logger = LoggerFactory.getLogger(ExperienceSignUpController.class);

    @Autowired
    private SiteService siteService;

    @Autowired
    private SmsSender smsSender;

    @Autowired
    private ExperienceSignUpService experienceSignUpService;

    /**
     * 
     * @return
     */
    @RequestMapping(value = "/offline/index", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public HttpResult offlineIndex() {
        HttpResult httpResult = new HttpResult();
        
        try {
            httpResult.success()
                      .data("sites", siteService.list())
                      .message("加载数据成功");
        } catch (Exception e) {
            logger.error("#ExperienceSignUpController index() occurs error", e);
            httpResult.fail("500").message("加载数据失败");
        }
        
        return httpResult;
    }

    /**
     * 
     * @param signUp
     * @return
     */
    @RequestMapping(value = "/offline", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public HttpResult signUpOffline(@RequestBody OfflineExperienceSignUp signUp,
                                    @RequestParam("siteId") Long siteId,
                                    @RequestParam("token") String token,
                                    @RequestParam("code") String code) {
        HttpResult httpResult = new HttpResult();
        
        try {
            if(StringUtils.isNotBlank(signUp.getMobile())) {
                String mobile = signUp.getMobile().replace("", "");
                
                boolean valid = smsSender.checkVerifyCode(mobile, code, token);
                
                if(valid) {
                    Site site = siteService.load(siteId);
                    experienceSignUpService.signUp(signUp.edit(site));
                    
                    httpResult.success()
                              .message("预约线下体验成功");
                } else {
                    httpResult.fail("500").message("验证码不正确");
                }
            } else {
                httpResult.fail("500").message("手机号不正确");
            }
        } catch (Exception e) {
            logger.error("#ExperienceSignUpController signUpOffline() occurs error", e);
            httpResult.fail("500").message("预约线下体验失败");
        }
        
        return httpResult;
    }

    /**
     * 
     * @param signUp
     * @return
     */
    @RequestMapping(value = "/online", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public HttpResult signUpOnline(@RequestBody OnlineExperienceSignUp signUp) {
        HttpResult httpResult = new HttpResult();
        
        try {
                experienceSignUpService.signUp(signUp);
                
                httpResult.success()
                          .message("预约线上体验成功");
        } catch (Exception e) {
            logger.error("#ExperienceSignUpController signUpOnline() occurs error", e);
            httpResult.fail("500").message("预约线上体验失败");
        }
        
        return httpResult;
    }
}
